﻿using System;
using System.Text;

namespace Networking.Runtime
{
    public abstract class XNetObject : IDisposable
    {
        protected bool _disposed { get;  set; } = false;
        public string InstanceName { get; set; }

        ~XNetObject()
        {
            if (!_disposed)
                Dispose(false);
        }

        public void Dispose()
        {
            if (!_disposed)
                Dispose(true);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _disposed = true;
                OnDispose();
                GC.SuppressFinalize(this);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Memory leak!!!(Type: {0}), (InstanceName: {1})", this.GetType().ToString(), InstanceName);
                throw new Exception(sb.ToString());
            }
        }

        protected abstract void OnDispose();
    }
}